package cqrtplm.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.model.constant.ProcessConstant;
import cqrtplm.service.PointSelectionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;

@CustomListener(category = "润通", description = "物料选点监听器")
@Slf4j
public class PointSelectionListener implements ExecutionListener {


    @Override
    public void notify(DelegateExecution delegateExecution) {
        log.info("----执行监听润通定制-物料选点下发SRM开始----");
        long l = System.currentTimeMillis();
        String workFlowContainerId = delegateExecution.getProcessInstanceBusinessKey();
        if (StringUtils.isBlank(workFlowContainerId)) {
            workFlowContainerId = delegateExecution.getVariable(ProcessConstant.WORK_FLOW_CONTAINER_ID).toString();
        }
        log.info("流程监听，物料选点下发SRM开始,id:{}", workFlowContainerId);
        String finalWorkFlowContainerId = workFlowContainerId;
        PointSelectionService pointSelectionService = SpringUtil.getBean(PointSelectionService.class);
        try {
            pointSelectionService.sendSRMPointSelectionData(finalWorkFlowContainerId);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("物料选点下发SRM失败:" + e.getMessage());
        }
        log.info("----执行监听润通定制-物料选点下发SRM结束，耗时:{}----", System.currentTimeMillis() - l);


    }
}
